/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.ems.service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.constant.Constant;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.ems.dao.CourseDao;
import com.thinkgem.jeesite.modules.ems.entity.Course;
import com.thinkgem.jeesite.modules.ems.entity.Order;

/**
 * 课程管理功能Service
 * @author zad
 * @version 2017-10-15
 */
@Service
@Transactional(readOnly = true)
public class CourseService extends CrudService<CourseDao, Course> {

	public Course get(String id) {
		return super.get(id);
	}
	
	public List<Course> findList(Course course) {
		return super.findList(course);
	}
	
	public Page<Course> findPage(Page<Course> page, Course course) {
		return super.findPage(page, course);
	}
	
	@Transactional(readOnly = false)
	public void save(Course course) {
		if (course.getRule()!=null){
			course.setRule(StringEscapeUtils.unescapeHtml4(course.getRule()));
		}
		super.save(course);
	}
	
	@Transactional(readOnly = false)
	public void delete(Course course) {
		super.delete(course);
	}
	
	public Map<String,Object> sumNumAndMoney(String joinType,BigDecimal oneMoney,BigDecimal multiMoney,String joiner,Order order)
	{
		Map<String,Object> map = new HashMap<String,Object>();
		/*if(StringUtils.isBlank(joinType))
		{
			map.put("success", "false");
			map.put("msg", "参赛类型为空。");
			return map;
		}
		else if(null == oneMoney || "".equals(oneMoney) || oneMoney.compareTo(new BigDecimal("0"))==0)
		{
			map.put("success", "false");
			map.put("msg", "参赛单价为空。");
			return map;
		}
		else if(null == multiMoney || "".equals(multiMoney) || multiMoney.compareTo(new BigDecimal("0"))==0)
		{
			map.put("success", "false");
			map.put("msg", "参赛集体单价为空。");
			return map;
		}
		else if(StringUtils.isBlank(joiner))
		{
			map.put("success", "false");
			map.put("msg", "参赛人员姓名为空。");
			return map;
		}
		
		if(joiner.endsWith(","))
		{
			map.put("success", "false");
			map.put("msg", "参赛人员姓名格式不正确。");
			return map;
		}
		*/
		int joinNum = joiner.split(",").length;
		BigDecimal singleMoney = new BigDecimal("0");
		if(Constant.Course.COURSE_JOIN_TYPE_MULTI.equals(joinType))
		{
			if(joinNum < Integer.valueOf(joinType))
			{
				map.put("success", "false");
				map.put("msg", "选择的参赛类型和输入的选手姓名不匹配，请重新输入");
				return map;
			}
			singleMoney = multiMoney;
		}
		else if(Integer.parseInt(joinType) < Integer.parseInt(Constant.Course.COURSE_JOIN_TYPE_MULTI))
		{
			if(!String.valueOf(joinNum).equals(joinType))
			{
				map.put("success", "false");
				map.put("msg", "选择的参赛类型和输入的选手姓名不匹配，请重新输入");
				return map;
			}
			singleMoney = oneMoney;
		}
		map.put("success", "true");
		map.put("joinNum", joinNum);
		if(null != order)
		{
			order.setNum(joinNum);
		}
		
		BigDecimal money = new BigDecimal(joinNum).multiply(singleMoney).setScale(2);
		if(null != order)
		{
			order.setMoney(money);
		}
		map.put("money", money);
		
		return map;
	}
	
}